package com.zhibang.xtwh.controller;

import com.zhibang.comnom.domain.AddRole;
import com.zhibang.comnom.domain.Menu;
import com.zhibang.comnom.domain.MenuTJ;
import com.zhibang.comnom.domain.Role;


import com.zhibang.comnom.re.Result;
import com.zhibang.comnom.re.ResultCode;
import com.zhibang.comnom.vo.MenuVo;
import com.zhibang.comnom.vo.RoleVo;
import com.zhibang.xtwh.service.XtwhService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@Api(tags="系统维护模块接口")
@RequestMapping("/xtwh")
public class XtwhController {
    @Resource
    private XtwhService xtwhServiceImp;
    @RequestMapping(value = "/menu" , method = RequestMethod.GET)
    @ApiOperation(value = "查询所有权限")
    public Result menu()throws Exception{
        List<Menu> menus = xtwhServiceImp.menu();
        return Result.ok( ResultCode.SUCCESS ,menus);
    }
    @RequestMapping(value = "/updatemenu",method = RequestMethod.POST)
    @ApiOperation(value = "修改权限")
    public Result updatemenu(MenuVo menuVo)throws Exception{
//        ObjectMapper objectMapper = new ObjectMapper();
//
//       MenuVo menuVo = null;
//        if (searchParams!=null){
//            menuVo=objectMapper.readValue(searchParams,MenuVo.class);
//            System.out.println(menuVo);
//        }
        System.out.println(menuVo);
        if (menuVo!=null){
            int i = xtwhServiceImp.updateMenu(menuVo);
            return Result.ok(ResultCode.SUCCESS,i);}
        else{
            return Result.fail(ResultCode.FAIL,0);
        }



    }
    @RequestMapping(value = "/MenuTJ",method = RequestMethod.POST)
    @ApiOperation(value = "添加权限")
    public Result MenuTJ(MenuTJ menuTJ)throws Exception{
        System.out.println(menuTJ);
        if(menuTJ!=null){
         int i = xtwhServiceImp.MenuTJ(menuTJ);
         return Result.ok(ResultCode.SUCCESS,i);
        }else {
            return Result.fail(ResultCode.FAIL,0);
        }
    }
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除权限")
    public Result DELETMenu( @PathVariable("id")Integer id)throws Exception{

      int i = xtwhServiceImp.DELETMenu(id);
        if(i>0){

            return Result.ok(ResultCode.SUCCESS,i);
        }else {
            return Result.fail(ResultCode.FAIL,0);
        }
    }
    @RequestMapping(value = "/roles" , method = RequestMethod.GET)
    @ApiOperation(value = "查询所有角色")
    public Result roles()throws Exception{
       List<Role> roles = xtwhServiceImp.Roles();
        return Result.ok( ResultCode.SUCCESS ,roles);
    }
    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    @ApiOperation(value = "添加角色")
    public Result MenuTJ(AddRole addRole)throws Exception{
        System.out.println(addRole);
        if(addRole!=null){
            int i = xtwhServiceImp.addRole(addRole);
            return Result.ok(ResultCode.SUCCESS,i);
        }else {
            return Result.fail(ResultCode.FAIL,0);
        }


}
    @RequestMapping(value = "/updaterole",method = RequestMethod.POST)
    @ApiOperation(value = "修改角色")
    public Result updatemenu(RoleVo roleVo)throws Exception {
//        ObjectMapper objectMapper = new ObjectMapper();
//
//       MenuVo menuVo = null;
//        if (searchParams!=null){
//            menuVo=objectMapper.readValue(searchParams,MenuVo.class);
//            System.out.println(menuVo);
//        }
        System.out.println(roleVo);
        if (roleVo != null) {
            int i = xtwhServiceImp.updateRole(roleVo);
            return Result.ok(ResultCode.SUCCESS, i);
        } else {
            return Result.fail(ResultCode.FAIL, 0);
        }
    }
    }
